﻿using System;
using System.Collections.Generic;
using System.Text;
using DTO;
using DAO;

namespace BUS
{
    public class BC_MDP_ChiTietBUS
    {
        public static bool LapCTBC(long mBC_MDP, long mP)
        {
            return BC_MDP_ChiTietDAO.LapCTBC(mBC_MDP, mP);
        }

        public static bool Update(long mBC_MDP, long mP, long sN, double tL)
        {
            return BC_MDP_ChiTietDAO.Update(mBC_MDP, mP, sN, tL);
        }

        public static List<BC_MDP_ChiTietDTO> LayDSTheoMBC(long mBC_MDP)
        {
            return BC_MDP_ChiTietDAO.LayDSTheoMBC(mBC_MDP);
        }

        public static void TinhSN(long mBC_MDP, long tBC)
        {
            List<BC_MDP_ChiTietDTO> L = BC_MDP_ChiTietBUS.LayDSTheoMBC(mBC_MDP);
            List<long> tSN = new List<long>();
            List<double> tL = new List<double>();

            for (int i = 0; i < L.Count; i++)
            {
                long sN = 0;
                List<HD_ChiTietDTO> HD = HD_ChiTietBUS.LayDSTheoMP(L[i].MaPhong.MaPhong);

                for (int j = 0; j < HD.Count; j++)
                {
                    PhieuThueDTO PT = PhieuThueBUS.LayPTTheoMPT(HD[j].MaPhieuThue.MaPhieuThue);
                    long m = (long)PT.NgayThue.Month;
                    if (m == tBC)
                        sN = sN + HD[j].SoNgayThue;
                }

                tSN.Add(sN);
            }

            long t = 0;

            for (int i = 0; i < tSN.Count; i++)
            {
                t = t + tSN[i];
            }

            for (int i = 0; i < tSN.Count; i++)
            {
                double temp = 0;

                temp = (double)tSN[i] / t * 100;
                tL.Add(temp);
            }

            for (int i = 0; i < tSN.Count; i++)
            {
                BC_MDP_ChiTietBUS.Update(mBC_MDP, L[i].MaPhong.MaPhong, tSN[i], tL[i]);
            }
        }
    }
}
